package controller;

import model.Chapter;
import model.MySql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Scanner;

/**
 * Created by yvdedu.com on 2016/10/10.
 */

@WebServlet(urlPatterns = "/get_look")
public class get_look extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String chapter_id=req.getParameter("chapter_id");
        MySql mySql=new MySql();
        Chapter chapter=mySql.getChapterById(Integer.parseInt(chapter_id));
        String url="C:\\Users\\yvdedu.com\\IdeaProjects\\小说\\book\\"+chapter.getPath();
        File file=new File(url);
        if (file.exists()){
            InputStream inputStream=new FileInputStream(file);
            byte[] bytes=new byte[1024];
            int size;
            OutputStream outputStream=resp.getOutputStream();
            while ((size=inputStream.read(bytes,0,1024))!=-1){
                outputStream.write(bytes,0,size);
            }
        }

    }
}
